"""
Python 数组、集合和遍历示例
"""

# ==================== 1. 列表 (List) - 类似数组 ====================
print("=" * 50)
print("1. 列表 (List)")
print("=" * 50)

# 定义列表
numbers = [1, 2, 3, 4, 5]
fruits = ["苹果", "香蕉", "橙子"]
mixed = [1, "hello", 3.14, True]  # 可以包含不同类型

print(f"数字列表: {numbers}")
print(f"水果列表: {fruits}")
print(f"混合列表: {mixed}")

# 列表操作
fruits.append("葡萄")  # 添加元素
print(f"添加后: {fruits}")

# 遍历列表的方法
print("\n--- 遍历列表 ---")

# 方法1: 直接遍历元素
print("方法1: 直接遍历")
for fruit in fruits:
    print(f"  {fruit}")

# 方法2: 使用索引遍历
print("\n方法2: 使用索引")
for i in range(len(fruits)):
    print(f"  索引 {i}: {fruits[i]}")

# 方法3: 同时获取索引和值
print("\n方法3: enumerate() - 同时获取索引和值")
for index, fruit in enumerate(fruits):
    print(f"  {index}: {fruit}")

# 方法4: 列表推导式
print("\n方法4: 列表推导式")
squared = [x**2 for x in numbers]
print(f"  平方数: {squared}")


# ==================== 2. 集合 (Set) ====================
print("\n" + "=" * 50)
print("2. 集合 (Set)")
print("=" * 50)

# 定义集合 - 无序、不重复
my_set = {1, 2, 3, 4, 5}
fruit_set = {"苹果", "香蕉", "橙子"}

# 自动去重
duplicate_set = {1, 2, 2, 3, 3, 3, 4}
print(f"去重后的集合: {duplicate_set}")  # {1, 2, 3, 4}

# 集合操作
fruit_set.add("葡萄")
print(f"添加后: {fruit_set}")

# 遍历集合
print("\n--- 遍历集合 ---")
for item in fruit_set:
    print(f"  {item}")

# 集合运算
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

print(f"\n集合1: {set1}")
print(f"集合2: {set2}")
print(f"并集: {set1 | set2}")
print(f"交集: {set1 & set2}")
print(f"差集: {set1 - set2}")


# ==================== 3. 元组 (Tuple) ====================
print("\n" + "=" * 50)
print("3. 元组 (Tuple) - 不可变的列表")
print("=" * 50)

# 定义元组
coordinates = (10, 20)
colors = ("红", "绿", "蓝")

print(f"坐标: {coordinates}")
print(f"颜色: {colors}")

# 遍历元组
print("\n--- 遍历元组 ---")
for color in colors:
    print(f"  {color}")


# ==================== 4. 字典 (Dictionary) ====================
print("\n" + "=" * 50)
print("4. 字典 (Dictionary) - 键值对")
print("=" * 50)

# 定义字典
person = {
    "name": "张三",
    "age": 25,
    "city": "北京"
}

student_scores = {
    "张三": 95,
    "李四": 87,
    "王五": 92
}

print(f"人物信息: {person}")
print(f"学生成绩: {student_scores}")

# 遍历字典的方法
print("\n--- 遍历字典 ---")

# 方法1: 遍历键
print("方法1: 遍历键")
for key in person:
    print(f"  {key}: {person[key]}")

# 方法2: 遍历键（明确）
print("\n方法2: keys() - 遍历键")
for key in person.keys():
    print(f"  {key}")

# 方法3: 遍历值
print("\n方法3: values() - 遍历值")
for value in person.values():
    print(f"  {value}")

# 方法4: 遍历键值对
print("\n方法4: items() - 遍历键值对")
for key, value in person.items():
    print(f"  {key} = {value}")


# ==================== 5. 其他遍历技巧 ====================
print("\n" + "=" * 50)
print("5. 其他遍历技巧")
print("=" * 50)

# while 循环遍历
print("while 循环:")
i = 0
while i < len(numbers):
    print(f"  numbers[{i}] = {numbers[i]}")
    i += 1

# zip() - 同时遍历多个列表
print("\nzip() - 同时遍历多个列表:")
names = ["张三", "李四", "王五"]
ages = [25, 30, 28]
for name, age in zip(names, ages):
    print(f"  {name}: {age}岁")

# reversed() - 反向遍历
print("\nreversed() - 反向遍历:")
for fruit in reversed(fruits):
    print(f"  {fruit}")

# sorted() - 排序后遍历
print("\nsorted() - 排序后遍历:")
unsorted = [3, 1, 4, 1, 5, 9, 2]
for num in sorted(unsorted):
    print(f"  {num}")


# ==================== 6. NumPy 数组（科学计算）====================
print("\n" + "=" * 50)
print("6. NumPy 数组（需要安装: pip install numpy）")
print("=" * 50)

# try:
#     import numpy as np
    
#     # 创建 NumPy 数组
#     arr = np.array([1, 2, 3, 4, 5])
#     matrix = np.array([[1, 2, 3], [4, 5, 6]])
    
#     print(f"一维数组: {arr}")
#     print(f"二维数组:\n{matrix}")
    
#     # 遍历 NumPy 数组
#     print("\n遍历 NumPy 数组:")
#     for element in arr:
#         print(f"  {element}")
    
#     print("\n遍历二维数组:")
#     for row in matrix:
#         print(f"  {row}")
        
# except ImportError:
#     print("NumPy 未安装。运行: pip install numpy")


print("\n" + "=" * 50)
print("示例结束！")
print("=" * 50)

